﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Work.Hardware;
using UeyesAIS.DataDefinition;
using UeyesAIS.Interface;

namespace UeyesAIS.Service.Device
{
    internal class DeviceControl0176 : BaseDeviceControl
    {
        private readonly DeviceParameter _deviceParameter;
        private WControl _sideUpLight;
        private WControl _backLight;
        public SignalCard IOCard;//控制点灯机上电、切图
        public DeviceControl0176(DeviceParameter dp)
        {
            _deviceParameter = dp;
            Init();
        }

        private void Init()
        {
            try
            {
                InitLightSource();
                InitEle();
                InitCamera();
                SideUpLightS = DeviceStatusEnum.Normal;
                SideDownLightS = DeviceStatusEnum.Normal;
                BackLightS = DeviceStatusEnum.Normal;
            }
            catch (Exception ex)
            {
                throw new Exception (string.Format("初始化失败硬件！\n{0}", ex.Message));
            }
        }

        private void InitCamera()
        {
            try
            {
                var count = _deviceParameter.CameraList.Count(x => x.Enable);
                var initc = new List<Task>();
                Cameras = new List<ICameraAdapter>();
                foreach (var cInfo in _deviceParameter.CameraList)
                {
                    if (cInfo.Enable)
                    {
                        var c = new CameraEbus(cInfo);
                        Cameras.Add(c);
                        initc.Add(c.Init());
                    }
                }
                Task.WaitAll(initc.ToArray(), 5000);
                foreach (var ca in Cameras)
                {
                    if (ca.CameraState && ImageReceiveed != null)
                    {
                        ca.ImageReady += ImageReceiveed;
                    }
                    else
                    {
                        CameraS = DeviceStatusEnum.Unknow;
                    }
                }
                if (CameraS != DeviceStatusEnum.Unknow)
                {
                    CameraS = DeviceStatusEnum.Normal;
                }
            }
            catch (Exception ex)
            {
                CameraS = DeviceStatusEnum.Unknow;
                CameraMsg = string.Format("相机初始化异常:{0}", ex.Message);
                //BpjbCommon.Net.Core.BpEventHandler.ErrHandler(string.Format(Device.Parameter.CameraList[0].CameraName + "连接失败！\n" + ex.Message), BpjbCommon.Net.Core.ErrType.Warning);
            }
        }

        private void InitEle()
        {
            try
            {
                IOCard = new SignalCard(_deviceParameter.IOCardComName);
                ElectricS = DeviceStatusEnum.Normal;
            }
            catch (Exception ex)
            {
                ElectricS = DeviceStatusEnum.Unknow;
                ETSMsg = CameraMsg = string.Format("点灯机初始化异常:{0}", ex.Message);
            }
        }

        private void InitLightSource()
        {
            try
            {
                _sideUpLight = new WControl(_deviceParameter.DustLightCom, WControl.ControlType.PD2);
                SideUpLightS = DeviceStatusEnum.Normal;
                SideDownLightS = DeviceStatusEnum.Normal;
                _backLight = new WControl(_deviceParameter.BackLightCom, WControl.ControlType.PD2);
                BackLightS = DeviceStatusEnum.Normal;
            }
            catch (Exception e)
            {
                SideUpLightS = DeviceStatusEnum.Normal;
                BackLightS = DeviceStatusEnum.Normal;
                SideDownLightS = DeviceStatusEnum.Normal;
                Console.WriteLine(e);
                throw;
            }
        }

        public override bool Begin()
        {
            IoOutput(_deviceParameter.SideLight1Channle, 0);
            Thread.Sleep(50);
            IoOutput(_deviceParameter.SideLight1Channle, 1);
            return true;
        }

        public override void ChangeColor(int index)
        {
            IoOutput(_deviceParameter.IOODownSwitchPic, 0);
            Thread.Sleep(50);
            IoOutput(_deviceParameter.IOODownSwitchPic, 1);
        }
        public void IoOutput(byte ch, byte output)
        {
            try
            {
                if (IOCard != null)
                {
                    IOCard.SetOutIO(ch, output);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("IO卡输出Error：" + ex.Message);
            }
        }
        public override void CloseBackLight()
        {
            _backLight.SetBright(1, 0);
        }

        public override void CloseDownSideLight()
        {
            _sideUpLight.SetBright(2, 0);
            Thread.Sleep(50);
            _sideUpLight.SetBright(3, 0);
        }

        public override void CloseUpSideLight()
        {
            _sideUpLight.SetBright(0, 0);
            Thread.Sleep(50);
            _sideUpLight.SetBright(1, 0);
            Thread.Sleep(50);
            _sideUpLight.SetBright(1, 0);
        }

        public override void End()
        {
            //
        }

        public override void OpenBackLight(byte inten)
        {
            _backLight.SetBright(1, inten);
        }

        public override void OpenDownSideLight(byte inten)
        {
            _sideUpLight.SetBright(2, inten);
            Thread.Sleep(50);
            _sideUpLight.SetBright(3, inten);
        }

        public override void OpenUpSideLight(byte inten)
        {
            _sideUpLight.SetBright(0, inten);
            Thread.Sleep(50);
            _sideUpLight.SetBright(1, inten);
        }
    }
}
